package com.dhy.util;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * @author 大忽悠
 * @create 2022/5/26 19:41
 */
@Slf4j
public class GitUtil {
    private static final YamlUtil yamUtil=new YamlUtil("git.yml");
    private static Git git;
    private static UsernamePasswordCredentialsProvider provider;

    static {

        provider=new UsernamePasswordCredentialsProvider(yamUtil.get("git.username"), yamUtil.get("git.password"));
    }

    public static Git openGit(String dir){
        try {
            Repository repository = new FileRepositoryBuilder()
                    .setGitDir(Paths.get(dir, ".git").toFile())
                    .build();
            git=new Git(repository);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return git;
    }

    public static File gitClone(String url,String branch,String localDir){
        try {
            git = Git.cloneRepository()
                    //设置git远端URL
                    .setURI(url)
                    //设置本地仓库位置
                    .setDirectory(new File(localDir))
                    //设置身份验证
                    .setCredentialsProvider(provide())
                    //设置是否克隆子仓库
                    .setCloneSubmodules(true)
                    //设置克隆分支
                    .setBranch(branch)
                    //启动命令
                    .call();
            return git.getRepository().getDirectory();
        } catch (GitAPIException e) {
            throw new RuntimeException(e);
        }
    }

   public static void gitPull(String branch){
       try {
           git.pull()
                   .setRemote("original")
                   //设置需要pull的远端分支
                   .setRemoteBranchName(branch)
                   //身份验证
                   .setCredentialsProvider(provide())
                   .call();
       } catch (GitAPIException e) {
           throw new RuntimeException(e);
       }
   }

   public static void gitAdd(String filePattern){
       try {
           git.add().addFilepattern(filePattern).call();
       } catch (GitAPIException e) {
           throw new RuntimeException(e);
       }
   }

   public static void gitCommit(String msg){
       try {
           git.commit().setMessage(msg).call();
       } catch (GitAPIException e) {
           throw new RuntimeException(e);
       }
   }

   public static void gitPush(String branch){
       try {
           git.push()
                   .setRemote("origin")
                   //设置需要推送的分支,如果远端没有则创建
                   .setRefSpecs(new RefSpec(branch))
                   //身份验证
                   .setCredentialsProvider(provide())
                   .call();
       } catch (GitAPIException e) {
           throw new RuntimeException(e);
       }
   }

   public static void close(){
        if(git!=null){
            git.close();
        }
   }

    private static UsernamePasswordCredentialsProvider provide() {
        return provider;
    }
}
